package com.example.weight.greenSeasons.deodorization;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.weight.R;
import com.example.weight.config.Config;
import com.example.weight.greenSeasons.waterDeal.DeodorizationTable;
import com.example.weight.util.Utility;
import com.example.weight.wormHandle.cultivate.SmallWormBreedingIncoming;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class D4Activity extends AppCompatActivity {
    private TitleBar titleBar;
    private String name,factory;
    private TextView tv_date;
    private EditText et_area, et_name, et_match, et_amount;
    private Button btn_save;

    Calendar calendar= Calendar.getInstance(Locale.CHINA);
    @SuppressLint("SimpleDateFormat")
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_d4);

        Intent previousIntent = getIntent();
        name = previousIntent.getStringExtra("name");
        factory = previousIntent.getStringExtra("factory");

        titleBar=findViewById(R.id.titleBar);
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                D4Activity.this.finish();
            }
            @Override
            public void onTitleClick(View v) {}
            @Override
            public void onRightClick(View v) {}
        });

        tv_date = findViewById(R.id.tv_date);
        et_area = findViewById(R.id.et_area);
        et_name = findViewById(R.id.et_name);
        et_match = findViewById(R.id.et_match);
        et_amount = findViewById(R.id.et_amount);
        btn_save = findViewById(R.id.btn_save);

        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });

        tv_date.setText(simpleDateFormat.format(new Date()));
        tv_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utility.showDatePickerDialog(D4Activity.this, 2, tv_date, calendar);
            }
        });



    }

    public void save() {
        if (et_area.getText().toString().equals("")) {
            Toast.makeText(this, "请填写消杀范围", Toast.LENGTH_SHORT).show();
            return;
        }
        if (et_name.getText().toString().equals("")) {
            Toast.makeText(this, "请填写消杀药品名称", Toast.LENGTH_SHORT).show();
            return;
        }
        if (et_match.getText().toString().equals("")) {
            Toast.makeText(this, "请填写配比度", Toast.LENGTH_SHORT).show();
            return;
        }
        if (et_amount.getText().toString().equals("")) {
            Toast.makeText(this, "请填写使用量", Toast.LENGTH_SHORT).show();
            return;
        }

        new AlertDialog.Builder(this)
                .setTitle("确定保存吗？")
                .setCancelable(false)
                .setNegativeButton("取消", ((dialog, which) ->
                        Toast.makeText(this, "取消保存", Toast.LENGTH_SHORT).show()))
                .setPositiveButton("确定", (dialog, which) -> {
                    //提交请求
                    RequestQueue queue = Volley.newRequestQueue(this);
                    String url = Config.url + "/sjq/deodorizationRunningFour/addForm";
                    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                            new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    try {
                                        JSONObject jsonObject = new JSONObject(response);
                                        String code = jsonObject.getString("code");
                                        if (code.startsWith("2")) {
                                            Toast.makeText(D4Activity.this,
                                                    "保存成功", Toast.LENGTH_SHORT).show();
                                            Intent intent = new Intent(D4Activity.this, SmallWormBreedingIncoming.class);
                                            startActivity(intent);
                                            D4Activity.this.finish();
                                        } else
                                            Toast.makeText(D4Activity.this,
                                                    "保存失败", Toast.LENGTH_SHORT).show();
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(D4Activity.this, "保存失败", Toast.LENGTH_SHORT).show();
                            Log.d("LB", error.toString());
                        }
                    }) {
                        @Override
                        protected Map<String, String> getParams() throws AuthFailureError {
                            Map<String, String> map = new HashMap<>();
                            map.put("date", tv_date.getText().toString());
                            map.put("killRange", et_area.getText().toString());
                            map.put("killDrugName", et_name.getText().toString());
                            map.put("proportioningDegree", et_match.getText().toString());
                            map.put("consumption", et_amount.getText().toString());
                            map.put("factory", factory);
                            return map;
                        }
                    };
                    stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                            DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                    queue.add(stringRequest);
                })
                .show();
    }
}